package cn.xiaomubiao.oschine.adapter.tweets_pager_adapter;

import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.base.BaseRecyclerViewAdapter;
import cn.xiaomubiao.oschine.bean.Tweet;
import de.hdodenhof.circleimageview.CircleImageView;

import static cn.xiaomubiao.oschine.util.UIUtils.getContext;

/**
 * Created by hasee on 2017/4/5.
 */

public class TweetsBaseRecyclerAdapter extends BaseRecyclerViewAdapter {

    List mTweets;
    private MyOnClickListener mListener;

    public TweetsBaseRecyclerAdapter( List list ) {
        mTweets = list;

    }

    public interface MyOnClickListener {
        void onClick( View v, int position );
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder( ViewGroup parent, int viewType ) {
        View itemView = LayoutInflater.from(getContext()).inflate(R.layout.view_tweets_pager_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position ) {
        Tweet tweet = (Tweet) mTweets.get(position);
        final ViewHolder vH = (ViewHolder) holder;
        vH.mTvUsername.setText(tweet.getAuthor());


        Spanned spanned = Html.fromHtml(tweet.getBody());

        vH.mTvContent.setText(spanned);


        vH.mTvTime.setText(tweet.getPubDate());
        if (tweet.getPortrait().length() > 0) {
            Glide.with(getContext()).load(tweet.getPortrait()).into(vH.mIvHeaderPhoto);
        } else {
            Glide.with(getContext()).load(R.drawable.widget_dface).into(vH.mIvHeaderPhoto);
        }
        if (!TextUtils.isEmpty(tweet.getImgSmall())) {
            vH.mIvItemPhoto.setVisibility(View.VISIBLE);
            Glide.with(getContext()).load(tweet.getImgSmall()).into(vH.mIvItemPhoto);
        } else {
            vH.mIvItemPhoto.setVisibility(View.GONE);
        }

        vH.mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                if (mListener != null) {
                    mListener.onClick(v, vH.getLayoutPosition() - 1);
                }
            }
        });

    }

    public void setTweetsOnItemClickListener( MyOnClickListener l ) {
        mListener = l;
    }


    @Override
    public int getItemCount() {
        return mTweets != null ? mTweets.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_header_photo)
        CircleImageView mIvHeaderPhoto;
        @BindView(R.id.tv_username)
        TextView mTvUsername;
        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.iv_review)
        ImageView mIvReview;
        @BindView(R.id.iv_good)
        ImageView mIvGood;
        @BindView(R.id.iv_item_photo)
        ImageView mIvItemPhoto;
        @BindView(R.id.ll_click)
        LinearLayout mLinearLayout;

        public ViewHolder( View itemView ) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }


}
